<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,maximum-scale=1.0" />
    <title>修改转盘</title>
    <#include "/view/admin/head.html"/>
    <script type="text/javascript" src="${base}/jstool/bootstrap-switch/bootstrap-switch.min.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/jstool/bootstrap-switch/bootstrap-switch.min.css" />

    <script type="text/javascript" src="${base}/jstool/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript" src="${base}/jstool/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/jstool/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css" />


    <script type="text/javascript" src="${base}/jstool/bootstrap-validator-0.5.3/js/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="${base}/jstool/bootstrap-validator-0.5.3/lang/zh_CN.min.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/jstool/bootstrap-validator-0.5.3/css/bootstrapValidator.min.css" />

    <script type="text/javascript" src="${base}/jstool/bootstrap-fileinput/js/fileinput.js"></script>
    <script type="text/javascript" src="${base}/jstool/bootstrap-fileinput/js/locals/zh.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/jstool/bootstrap-fileinput/css/fileinput.min.css" />


    <script type="text/javascript" src="${base}/jstool/bootstrap-select/js/bootstrap-select.min.js"></script>
    <script type="text/javascript" src="${base}/jstool/bootstrap-select/lang/defaults-zh_CN.min.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/jstool/bootstrap-select/css/bootstrap-select.min.css" />


    <style type="text/css">
        .list-path {
            height: 50px;
            line-height: 50px;
            background-color: #9acfea;
        }

        .table-class {
            width: 100%;
            text-align: center;
            border: 1px solid #afd9ee;
            margin: 0 auto;
            margin-top: 50px;
        }

        table th {
            background-color: #d9edf7;
            text-align: center;
        }

        table td {
            /*height:100px;*/
            text-align: center;
        }


    </style>
    <script type="text/javascript">

        $(function(){
            //初始化
            $('.picPath').fileinput({
                language: 'zh', //设置语言
                allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
                showUpload: false, //是否显示上传按钮
                showCaption: false,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            });


        });
        
        //提交图片
        function submitVoteOrder(){
      	    	  	  	  	  	             	
      	   var probabilitys = $("input[name='probability']");
      	   
      	   var num = 0;
      	   probabilitys.each(function(){
      		   num = num*1 + this.value*1;
      	   });
      	    
      	    console.log(num);
      	    console.log(Math.round(num));
      	    console.log(parseInt(num));
      	   if(num >1.01 || num <0.99 ){
      		   alert("概率之和必须等于1");
      		   return;
      	   }
      	   	   	      
      	   var thingIds =$("select[name='thingId']");
      	   var thingIdsindex = false;
      	   thingIds.each(function(){
      		   if(this.value==-1){
      			   thingIdsindex =true;			  
      		   }		   
      	   });	  
      	   
      	   if(thingIdsindex){
      		   alert("奖品不能为空"); 
      		   return;
      	   }
      	   
      	   var countnums =$("input[name='count']");
      	   
      	   var countnumsindex = false;
      	   
      	   countnums.each(function(){
      		   if(this.value==""){
      			   countnumsindex =true;
      		   }		   
      	   });	 
      	   
      	   if(countnumsindex){
      		   alert("分配数量不能为空"); 
      		   return; 
      	   }
      	   
      	  
      	  document.getElementById("czjrouletteForm").submit(); 	  	  	  	  	  	  
      	 
        }        

    </script>
</head>
<body>
<div class="list-path" > <span style="margin-left: 50px;"><span class="glyphicon glyphicon-home"></span>
    优惠活动/奖品分配
</span></div>

<hr/>


<table class="table table-bordered" style="width:500px;">
    <tr>
        <th colspan="2">未修改前记录</th>

    </tr>
    <tr>
        <th>奖品</th>
        <th>分配数量</th>
    </tr>
    <#list list as entity>
        <#if entity.thingId??>
        <tr>
            <td>
                <#list listAwardId as lv>
                    <#if entity.thingId??&&entity.thingId == lv.value >
                        ${lv.name!}
                    </#if>
                </#list>
            </td>
            <td>
                ${entity.count?default(0)}
            </td>
        </tr>
        </#if>
    </#list>
</table>
<hr/>
<div class="table-class" style="margin-top:0px;">


    <#include "/view/admin/opermsg.html"/>
        <form action="${base}/roulette/saveAwardon.do" id="czjrouletteForm"
    method="post" enctype="multipart/form-data" >
            <input type="text" hidden name="rouletteId" value="${rouletteId!}"/>
        <table class="table table-bordered" >
            <tr>
                <th colspan="8">要修改内容</th>
            </tr>
        <tr>
            <th style="display:none;">主键</th>
            <th >奖品序号</th>
            <th style="width:200px;">奖品</th>
            <th>库存剩余数量</th>
            <th style="width:30px;">分配数量</th>
            <!--<th style="width:30px;">已中奖数量</th>-->
            <th style="width:30px;">奖品概率</th>


            <th style="width:100px;">中奖说明</th>
            <th style="width:100px;">中奖补充说明</th>
        </tr>

            <#list list as entity>
            <tr>
                <td hidden><input type="text" name="awardonId" value="${entity.awardonId!}"> </td>
                <td style="max-width:50px;"><input type="text" name="ext1" value="${entity.ext1!}" style="max-width:30px;"> </td>
                <td>
                    <!--<div class="input-group"  style="width:200px;">-->

                        <select id="s_${entity.awardonId!}" name="thingId" class="form-control selectpicker"   data-live-search="true"  onchange="javascript:businessChange('${entity.awardonId!}')" >
                            <option  value="">请选择奖品</option>
                          <#list listAwardId as lv>
                                <option value="${lv.value!}"
                                <#if entity.thingId??&&entity.thingId == lv.value >
                                    selected="selected"
                                </#if>
                                >${lv.name!}</option>
                            </#list>
                        </select>

                    <!--</div>-->
                </td>

                <td>
                    <span id="sp_${entity.awardonId!}">${entity.distributable!}</span>
                    <input type="hidden" id="count_${entity.awardonId!}" value="${entity.count!}"/>
                    <input type="hidden" id="thingId_${entity.awardonId!}" value="${entity.thingId!}"/>

                </td>
                <td><input type="text" id="ucount_${entity.awardonId!}" name="count" value="${entity.count!}"  style="max-width:50px;"></td>
                <td><input type="text" name="probability" value="${entity.probability!}"  style="max-width:50px;"></td>
                <!--<td>${entity.ext3?default(0)}</td>-->
             <!--   <td>
                    <select name="theme" >
                        <option value="0" <#if entity.theme??&&'0'==entity.theme>selected="selected"</#if>>否</option>
                        <option value="1" <#if entity.theme??&&'1'==entity.theme>selected="selected"</#if>>是</option>
                    </select>
                </td>-->

                <td>
                    <#if entity.prizePath??><img src="${base}${entity.prizePath!}" height="100px;">
                    <#else>[未设置]</#if>

                    <div class="form-group">
                    <input type="file" class="form-control picPath"  name="prizePath"  height="100px">
                    </div>
                </td>
                <td>
                    <#if entity.prizePath??><img src="${base}${entity.backgroundPath!}" height="100px;">
                        <#else>[未设置]</#if>
                    <div class="form-group">
                    <input type="file" class="form-control picPath"  name="backgroundPath"  height="100px">
                    </div>
                </td>
            </tr>

                </#list>

            <tr>
                <td colspan="8" width="100%" style="text-align: center;">
                    <a class="btn btn-success  approve-btn btn-approve" href="czjrouletteEdit.do?rouletteId=${rouletteId!}">&nbsp;上一步&nbsp;</a>
                    <span >&nbsp;</span>
                    <button class="btn btn-success  approve-btn btn-approve" type="button" onclick="submitVoteOrder();">&nbsp;保存&nbsp;</button>
                    <span >&nbsp;</span>
                </td>
            </tr>

        </table>
        </form>



        
               
        </tbody>
    </table>


</div>

<script type="text/javascript">


    function businessChange(dataId) {

        var daId = $('#s_'+dataId).val();
        var thingIdElements = document.getElementsByName("thingId");
        var arr = new Array([thingIdElements.length]);
        for(var i=0;i<thingIdElements.length;i++){
            if(thingIdElements[i].value!=''){
                arr[i] = thingIdElements[i].value;
            }

        }

        if(isRepeat(arr)){
            alert('不能选择重复奖品');
            $('#s_'+dataId).val("");
            $('#sp_'+dataId).text("0");
//            $('#s_'+dataId).parent().parent().findClass('filter-option pull-left').text(1);
            $('#s_'+dataId).parent().children('div').children('button').children('.filter-option').text('请选择奖品');
            $('#s_'+dataId).parent().children('div').children('button').attr('title','请选择奖品');
            $('#s_'+dataId).parent().children('div').find('ul li').removeAttr('class');

            return ;
//            return false;
        }


        var html = "";
        var getSY = "getAwardstockSY.do?thingId=" + daId;

        $.ajax({
            type : "POST",
            contentType : "application/json",
            url : getSY,
            dataType : 'json',
            success : function(result) {
                $('#sp_'+dataId).text(parseInt(result.shengyuNum));
            }

        });
    }
    function isRepeat(arr){
        var hash = {};
        for(var i in arr) {
            if(hash[arr[i]])
                return true;
            hash[arr[i]] = true;
        }
        return false;
    }
</script>


</body>
</html>
